﻿namespace ImageProcessing.Forms
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    /// <summary>
    /// canny form
    /// </summary>
    public partial class CnnyForm : Form
    {
        /// <summary>
        /// canny object
        /// </summary>
        private Canny cannyData;

        /// <summary>
        /// Initializes a new instance of the <see cref="CnnyForm"/> class.
        /// </summary>
        /// <param name="oreginalImage">The oreginal image.</param>
        public CnnyForm(Bitmap oreginalImage)
        {
            this.InitializeComponent();
            this.IrisImage.Image = oreginalImage;
        }

        /// <summary>
        /// Handles the Click event of the BtnCannyEdgeDetect control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtnCannyEdgeDetect_Click(object sender, EventArgs e)
        {
            float tH, tL, sigma;
            int maskSize, n;
            tH = (float)Convert.ToDouble(this.TxtTH.Text);
            tL = (float)Convert.ToDouble(this.TxtTL.Text);

            sigma = (float)Convert.ToDouble(this.TxtSigma.Text);
            n = Convert.ToInt32(((3.7 * sigma) - 0.5));
            maskSize = (2 * n) + 1;
            this.cannyData = new Canny((Bitmap)this.IrisImage.Image, tH, tL, maskSize, sigma);

            this.HystThreshImage.Image = this.cannyData.DisplayImage(this.cannyData.NonMax1);

            this.GaussianFilteredImage.Image = this.cannyData.DisplayImage(this.cannyData.FilteredImage); // cussuina

            this.GNL.Image = this.cannyData.DisplayImage(this.cannyData.GNL);

            this.GNH.Image = this.cannyData.DisplayImage(this.cannyData.GNH);

            this.CannyEdges.Image = this.cannyData.DisplayImage(this.cannyData.EdgeMap);
        }

        /// <summary>
        /// Handles the Click event of the BtnApply control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtnApply_Click(object sender, EventArgs e)
        {
            Main.OperationName = "Canny filter!";
            if (this.CannyEdges.Image != null)
            {
                Main.ImageList[Main.CurrImage].UpdateImage((Bitmap)this.CannyEdges.Image);
            }
            else
            {
                float tH, tL, sigma;
                int maskSize, n;
                tH = (float)Convert.ToDouble(this.TxtTH.Text);
                tL = (float)Convert.ToDouble(this.TxtTL.Text);

                sigma = (float)Convert.ToDouble(this.TxtSigma.Text);
                n = Convert.ToInt32(((3.7 * sigma) - 0.5));
                maskSize = (2 * n) + 1;
                this.cannyData = new Canny((Bitmap)this.IrisImage.Image, tH, tL, maskSize, sigma);

                this.HystThreshImage.Image = this.cannyData.DisplayImage(this.cannyData.NonMax1);

                this.GaussianFilteredImage.Image = this.cannyData.DisplayImage(this.cannyData.FilteredImage); // cussuina

                this.GNL.Image = this.cannyData.DisplayImage(this.cannyData.GNL);

                this.GNH.Image = this.cannyData.DisplayImage(this.cannyData.GNH);

                this.CannyEdges.Image = this.cannyData.DisplayImage(this.cannyData.EdgeMap);
                Main.ImageList[Main.CurrImage].UpdateImage((Bitmap)this.CannyEdges.Image);

            }
           
           // Main.ImageList[Main.CurrImage].UpdateImage(this.cannyData.ReturnImageStructure(this.cannyData.EdgeMap));
            this.Close();
        }
    }
}
